top.DEV?null:top.DEV={}; 
top.DEV.mapPage = {
    mapcfg: null,
    map: null,
    mapWindow: window,
    mapElement: "map",
    mapWidgets: {},
    MAP_PANEL: null
};

require(["dojo/request/xhr", "dojox/xml/DomParser",
        "dojo/_base/lang",
        "dojo/_base/array",

        "esri/map", "esri/geometry/Extent",
        "tdt/TDTLayer",

        "widgets/base/coordinate",
                
        "dojo/domReady!"], 
    function(xhr, DomParser, lang, array,
        Map, Extent, TDTLayer, coordinate) {

        top.DEV.mapPage.MAP_PANEL = new $.Panel();
                
        xhr("config.xml", {
            handleAs: "text"
        }).then(function(data){
            var jsdom = dojox.xml.DomParser.parse(data);
            var mapcfg = {};
            mapcfg = xmlObj2JsObj(jsdom.childNodes[1].childNodes[1] /* mapXml*/, mapcfg);
            top.DEV.mapPage.mapcfg = mapcfg;

            initMap();
            initMapWidgets();
            initWidgetContainers();


        }, function(err){
            alert("error");
        }, function(evt){
        });

        // ----------- Init Functions -------------------------
        function initMap(){
            var cfg = top.DEV.mapPage.mapcfg;
            var hasSetExtent = false;
            top.DEV.mapPage.map = new Map(top.DEV.mapPage.mapElement);

            if(!cfg.basemaps.map){
                log("地图加载", "未找到地图配置信息！");
                return;
            }
            if(cfg.basemaps.map instanceof Array){
                array.forEach(cfg.basemaps.map, loadLayer);
            }else{
                loadLayer(cfg.basemaps.map);
            }

            function loadLayer(basemap){
                if(basemap.visible != '1')
                    return;
                require([getMapClassPath(basemap.type)], function(Layer){
                    var layer = new Layer(basemap.type == "TDT" ? basemap.mapType : basemap.url);
                    layer.id = basemap.id;
                    layer.group = basemap.group;
                    top.DEV.mapPage.map.addLayer(layer);
                    if(!hasSetExtent){
                        resetExtent();
                        hasSetExtent = true;
                    }
                });
            }
        }

        function resetExtent(){
            var extentCfg = top.DEV.mapPage.mapcfg.initExtent;
            var initExtent = new Extent(parseFloat(extentCfg.xmin), parseFloat(extentCfg.ymin),
                            parseFloat(extentCfg.xmax), parseFloat(extentCfg.ymax), 
                            top.DEV.mapPage.map.spatialReference);
            top.DEV.mapPage.map.setExtent(initExtent);
        }

        function initMapWidgets(){
            var cfg = top.DEV.mapPage.mapcfg.mapWidgets.widget;
            
            if(!cfg){
                log("地图部件加载", "未找到地图部件配置信息！");
                return;
            }
            if(cfg instanceof Array){
                array.forEach(cfg, loadWidget);
            }else{
                loadWidget(cfg);
            }

            function loadWidget(widget){
                require([widget.url], function(W){
                    var w = new W(top.DEV.mapPage.map, widget);
                    w.show();
                    top.DEV.mapPage.mapWidgets[widget.name] = w;

                });
            }
        }

        function initWidgetContainers(){
            var widgetContainerCfg = top.DEV.mapPage.mapcfg.widgetContainer;
            if(widgetContainerCfg instanceof Array){
                array.forEach(widgetContainerCfg, function(layout){
                    loadWidgetLayout(layout);
                });
            }else{
                loadWidgetLayout(widgetContainerCfg);
            }
            
            function loadWidgetLayout(layout){
                require([layout.url], function(L){
                    var l = new L(top.DEV.mapPage.map, layout);
                    if(layout.widget){
                        if(layout.widget instanceof Array){
                            array.forEach(layout.widget, function(wItem){
                                l.addItem(wItem);
                            });
                        }else{
                            l.addItem(layout.widget);
                        }
                    }
                    l.show();
                });
            }
        }

        // ----------- Functions -------------------------
        function xmlObj2JsObj(xmlObj){
            var obj = {};
        
            function parseAttributes(xml, js){
                array.forEach(xml.attributes, function(attr){
                    js[attr.nodeName] = attr.nodeValue;
                });
                return js;
            }

            function parseChild(xml, js){
                if(xml.childNodes.length < 1)
                    return js;
                
                array.forEach(xml.childNodes, function(cnode){
                    if(cnode.nodeType != 1)
                        return;
                    var cnodeJs = {};
                    cnodeJs = parseAttributes(cnode, cnodeJs);
                    cnodeJs = parseChild(cnode, cnodeJs);
                    if( js[cnode.nodeName] ){
                        if( js[cnode.nodeName] instanceof Array )
                            js[cnode.nodeName].push(cnodeJs);
                        else{
                            var tmpObj = {}; lang.mixin(tmpObj, js[cnode.nodeName]);
                            js[cnode.nodeName] = [tmpObj, cnodeJs];
                        }
                    }else{
                        js[cnode.nodeName] = cnodeJs;
                    }
                    
                });
                return js;
            }

            obj = parseAttributes(xmlObj, obj);
            return parseChild(xmlObj, obj);
        }

        function getMapClassPath(type){
            switch (type){
                case "TDT":
                    return "tdt/TDTLayer";
                default:
                    return "";
            }
        }

});


function log(label, value){
    if(console && console.log)
        console.log(label, value);
}

